Open a browser from a Java application on Windows, Unix, or Macintosh.
- Windows - Opens the url in the system browser by calling a url.dll that can open the url using FileProtocolHandler. This dll fails to open file urls with spaces. In that case, a url shortcut is written to the hard drive, and the shortcut is then opened.
- Macintosh - Opens the url using the MRJ classes provided by Apple. The MRJ toolkit for Mac OS X does not implement openURL() yet, so this class will not work with Mac OS X at the current time. When Apple implements this function, it will start working again. In the meantime, it might be possible to find a command line that will work. This class links to the MRJ dynamically at runtime, so this class will compile even on computers such as Windows and Linux which do not have the MRJ installed.
- Unix, Linux, and other systems - Checks to see if the following browsers are in the path using the 'which' command: Mozilla, Netscape, and Lynx. It can open the url in any of these browsers.
There is also a configuration dialog box (Swing) that can be used in GUI applications that use this class. This allows users to choose their own browser or command line which should be used to open the url. The default command line parsing in Java tokenizes only on spaces. This class uses a custom command line parser that allows quoted strings and escape characters so that the command line to the browser can be specified more easily on a variety of platforms.

The original idea for this class came from a JavaWorld Java Tip. That implementation has some serious problems:
- Security - With the JavaWorld example it would be possible to give the class a url to open that would fool the command line parser and possibly allow something other than the browser to be executed. (Any url with white space in it would break the example.) com.Ostermiller.util.Browser manipulates the url and urlencodes characters which might confuse the command line parser.
- Out of date - There are a variety of browsers available on Linux. It would be nice to check for something better than Netscape. com.Ostermiller.util.Browser checks for a variety of browsers.
- Blocking - If you are using Netscape under Linux, a Java program using this library will wait for Netscape to close before continuing. com.Ostermiller.util.Browser waits for one second to check if it is still running rather than blocking.
Example
Open this page from a java application
import com.Ostermiller.util.Browser;
...
Browser.init(); // only needs to be called once.
...
try {
Browser.displayURL("http://ostermiller.org/utils/Browser.html");
} catch (IOException x){
System.err.println(x.getMessage());
}
[Download /w Source |
Version History |
Browse Source |
Documentation]
Copyright (c) 2001 by Stephen Ostermiller
(utils@Ostermiller.com)
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.